package com.thunder.reader.html;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlContentExtractor {
    private static final String TAG = "HtmlContentExtractor";
    public static String extractTextFromHtml(String html) {
        if (html == null || html.length() < 1) {
            return "";
        }
        
        html = removeCommonTag(html);
        
        // Remove primary HTML tags
        Pattern tag = Pattern.compile("<[\\d\\D]*?>");
        Matcher mtag = tag.matcher(html);
        while (mtag.find()) html = mtag.replaceAll("");

        // Remove special characters, such as &nbsp;
        Pattern sChar = Pattern.compile("&[\\d\\D]*?;");
        Matcher msChar = sChar.matcher(html);
        while (msChar.find()) html = msChar.replaceAll("");

        return html;
    }

    public static String extractFromHtml(String html) {
        if (html == null || html.length() < 1) {
            return "";
        }
        
        html = removeCommonTag(html);
        
        // a little specific to text converted form ViewText.org
//        Log.e(TAG, "before head \n'" + html + "'\n");
        Pattern header = Pattern.compile("<head>[\\d\\D]+?</head>");
        Matcher mheader = header.matcher(html);
        if (mheader.find()) {
            html = mheader.replaceAll("");
        }
//        Log.e(TAG, "after head \n'" + html + "'\n");
        Pattern vtheader = Pattern.compile("<div\\s+?id=\"vt-header\">[\\d\\D]+?</div>");
        Matcher mvtheader = vtheader.matcher(html);
        if (mvtheader.find()) {
            html = mvtheader.replaceAll("");
        }
        Pattern title = Pattern.compile("<h1\\s+?id=\"vt-title\">[\\d\\D]*?</h1>");
        Matcher mtitle = title.matcher(html);
        if (mtitle.find()) {
            html = mtitle.replaceAll("");
        }
        Pattern url = Pattern.compile("<div>[\\w\\.]+?</div>");
        Matcher murl = url.matcher(html);
        if (murl.find()) {
            html = murl.replaceAll("");
        }
        Pattern takeaway = Pattern.compile("<span\\s+?class=\"c-6 heavy\">Takeaway:\\s+?</span>");
        Matcher mtakeaway = takeaway.matcher(html);
        if (mtakeaway.find()) {
            html = mtakeaway.replaceAll("");
        }

        return html;
    }

    private static String removeCommonTag(String html) {
        // Remove style tags & inclusive content
        Pattern style = Pattern.compile("<style[\\d\\D]*?>[\\d\\D]*?</style>");
        Matcher mstyle = style.matcher(html);
        while (mstyle.find()) html = mstyle.replaceAll("");

        // Remove script tags & inclusive content
        Pattern script = Pattern.compile("<script[\\d\\D]*?>[\\d\\D]*?</script>");
        Matcher mscript = script.matcher(html);
        while (mscript.find()) html = mscript.replaceAll("");

        // Remove comment tags & inclusive content
        Pattern comment = Pattern.compile("<!--[\\d\\D]*?-->");
        Matcher mcomment = comment.matcher(html);
        while (mcomment.find()) html = mcomment.replaceAll("");
        return html;
    }
}